﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using XF.FM;
using XF.FM.Core.Table;
using XF.FM.Table;
using XF.FM.TestEntities;

namespace XF.FM.Simple.Controllers
{
    public class TablesController : Controller
    {
        EfTestContext _dbContext;
        public TablesController(EfTestContext dbContext)
        {
            _dbContext = dbContext;
        }
        public IActionResult MyTable(PageRequest pageRequest)
        {
            var pageData = pageRequest.GetData(_dbContext.Film.AsNoTracking());
            //var data = _dbContext.Film.AsNoTracking().Skip(pageRequest.Start).Take(pageRequest.Length);
            return View(pageData);
        }
        public IActionResult Simple()
        {
            var d = _dbContext.Film.First(m => m.FilmId == 3);
            d.LastUpdate = DateTime.Now;
            var t = _dbContext.SaveChanges();
            return View();
        }
        public IActionResult Data()
        {
            ViewData["D1"] = DataSeeds.GetUsers();

            return View();
        }
        public IActionResult DataAjax_1(TableRequest tableRequest)
        {
            var query = _dbContext.Set<Film>().AsQueryable();
            var data = tableRequest.GetEfResult(query);
            return Json(data);
        }
        public IActionResult DataAjax_1_Sort(TableRequest tableRequest)
        {
            var query = _dbContext.Set<Film>().AsQueryable();
            var data = tableRequest.GetEfResult(query, false, m => m.Title);
            return Json(data);
        }

        public IActionResult DataAjax_2(TableRequest tableRequest)
        {
            tableRequest.ParseOrder(Request.Query);
            var query = _dbContext.Set<Film>().AsQueryable();
            var data = tableRequest.GetEfResult<Film, FilmVM>(query, m => new FilmVM()
            {
                Id = m.FilmId,
                Tl = m.Title,
                Rat = m.Rating,
                UTime = m.LastUpdate
            });
            // data.DT_RowID = "Id";
            JsonSerializerOptions option = new JsonSerializerOptions();
            option.PropertyNamingPolicy = null;
            return Json(data);
        }
        public IActionResult Jsgrid()
        {
            return View();
        }
        public class FilmVM
        {
            public int Id { get; set; }
            public string Tl { get; set; }
            public string Rat { get; set; }
            public DateTime UTime { get; set; }
        }
    }
}